package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.AttendanceList;
import com.example.prison.entity.vo.AttendanceListVO;
import com.example.prison.service.AttendanceListService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@Api(tags = "考勤管理")
@RestController
@RequestMapping("/api")
public class AttendanceListController {

    @Autowired
    private AttendanceListService attendanceListService;

//    @Autowired
//    private SystemLogService systemLogService;

    @ApiOperation("获取考勤记录")
    @GetMapping("getAttendanceList")
    public Response<PageInfo<AttendanceList>> getAttendanceList(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<AttendanceList> pageInfo = attendanceListService.getAttendanceList(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取考勤记录及信息")
    @GetMapping("getAttendanceListAndInfo")
    public Response<PageInfo<AttendanceListVO>> getAttendanceListAndInfo(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                         @RequestParam(value = "beginTime", defaultValue = "0000-00-00 00:00:00") String beginTime,
                                                                         @RequestParam(value = "endTime", defaultValue = "9999-12-31 23:59:59") String endTime) {
        if(beginTime.compareTo(endTime) > 0 ) {
            return Response.error("时间输入错误！");
        }
        PageInfo<AttendanceListVO> pageInfo = attendanceListService.getInfoList(pageNumber, pageSize, beginTime, endTime);
        return Response.success("获取成功！", pageInfo);
    }

//    @ApiOperation("依据时间获取考勤记录")
//    @GetMapping("getAttendanceListByTime")
//    public Response<PageInfo<AttendanceListVO>> getAttendanceListByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                            @RequestParam(value = "beginTime", defaultValue = "0000-00-00 00:00:00") String beginTime,
//                                                            @RequestParam(value = "endTime", defaultValue = "9999-12-31 23:59:59") String endTime) {
////        beginTime = beginTime + ":00";
////        endTime = endTime + ":00";
//        if(beginTime.compareTo(endTime) > 0 ) {
//            return Response.error("时间输入错误！");
//        }
//        PageInfo<AttendanceListVO> pageInfo = attendanceListService.getListByTime(pageNumber, pageSize, beginTime, endTime);
//        return Response.success("获取成功！", pageInfo);
//    }

    @ApiOperation("删除考勤记录")
    @PostMapping("/deleteAttendanceList")
    public Response<?> deleteAttendanceList(@Parameter(description = "考勤记录ID") @RequestParam Integer[] ids) {
        Integer[] res = {};
        for(Integer id : ids) {
            if (ObjectUtils.isEmpty(attendanceListService.getById(id))) {
                res = Arrays.copyOf(res,res.length+1);
                res[res.length-1] = id;
            }
        }
        if(ObjectUtils.isNotEmpty(res)) {
            return Response.info("安防记录id为"+ Arrays.toString(res) +"的记录不存在！",Arrays.toString(res));
        }
        attendanceListService.delete(ids);
        log.info("删除考勤记录");
//        systemLogService.addLog("删除考勤记录");
        return Response.success("删除成功！");
    }

}
